package com.zhuss.advice.processor;

import com.zhuss.advice.db.dao.ext.ComplaintDao;
import com.zhuss.bean.bo.advice.Complaint.SearchComplaintBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.advice.complaint.DetailComplaintVo;
import com.zhuss.bean.vo.advice.complaint.ListComplaintVo;
import com.zhuss.bean.vo.advice.complaintSon.ListComplaintSonVo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: zhushanshuo
 * @Date: 2020/3/6 16:15
 */
@Component("complaintRead")
public class ComplaintRead {

    @Autowired
    private ComplaintDao complaintDao;

    @Autowired
    private ComplaintSonRead complaintSonRead;

    @Autowired
    @Qualifier("downloadProcessor")
    private Processor<DownloadVo, DownloadBo> downloadProcessor;

    /**
     * 列表
     * @param bo
     */
    public Object list(SearchComplaintBo bo) {
        PageVo<ListComplaintVo> result = new PageVo<ListComplaintVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        if (bo.getCreateTime() != null){
            bo.setCreateTimeStart(DateUtil.toDateOnly(bo.getCreateTime()));
        }
        if (bo.getCreateTime() != null){
            bo.setCreateTimeEnd(DateUtil.toDateOnlyEnd(bo.getCreateTime()));
        }
        result.setItems(complaintDao.list(bo));
        result.setTotal(complaintDao.countList(bo));
        return result;
    }

    /**
     * 详情
     * @throws WpsException
     */
    public Object detail(Long complaintId) throws WpsException {
        DetailComplaintVo result = complaintDao.detail(complaintId);
        DownloadBo downloadBo = new DownloadBo();
        downloadBo.setKey(result.getComplaintKey());
        DownloadVo downloadVo = downloadProcessor.process(downloadBo);
        result.setComplaintUrl(downloadVo.getUrl());
        List<ListComplaintSonVo> sonVoList = null;
        try {
            sonVoList = (List<ListComplaintSonVo>)complaintSonRead.detail(complaintId);
        } catch (WpsException e) {
            if (ExceptionCode.数据不存在.getCode().intValue() != e.getCode().intValue()) {
                throw e;
            }
        }
        if (sonVoList != null){
            result.setSonVoList(sonVoList);
        }
        if (result == null) {
            throw new WpsException(ExceptionCode.数据不存在);
        }
        return result;
    }

    /**
     * 办理列表
     * @param bo
     * @return
     */
    public Object handleList(SearchComplaintBo bo) {
        PageVo<ListComplaintVo> result = new PageVo<ListComplaintVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        bo.setCreateId(bo.getConcurrentUserId());
        if (bo.getCreateTime() != null){
            bo.setCreateTimeStart(DateUtil.toDateOnly(bo.getCreateTime()));
        }
        if (bo.getCreateTime() != null){
            bo.setCreateTimeEnd(DateUtil.toDateOnlyEnd(bo.getCreateTime()));
        }
        result.setItems(complaintDao.handleList(bo));
        result.setTotal(complaintDao.handleCount(bo));
        return result;
    }
}
